package com.logic.landseaserver.ws;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.domain.AuditLog;
import com.logic.landseaserver.service.IAuditLogService;
import com.logic.landseaserver.service.utils.ResetQueryUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Path("/auditLog")
@Api(value="auditLog")
public class AuditLogRest {
	@Autowired
	private IAuditLogService auditLogServiceImpl;

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Create a new auditLog")
	public Response create(@ApiParam(value = "param", required = true) AuditLog auditLog) {
		CommonResultMap map = new CommonResultMap();
        try {
        	int id = auditLogServiceImpl.insert(auditLog);
        	auditLog.setId(id);
            map.setData(auditLogServiceImpl.get(id));
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("create error", e);
            map.setResult(e);
        } catch (Exception e) {
            logger.error("create error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}


    @GET
    @Path("/detail/{auditLogId}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Get auditLog by auditLog id")
    public Response retrieveDTO(
            @ApiParam(value = "param", required = true) @PathParam("auditLogId") Integer auditLogId) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(auditLogServiceImpl.get(auditLogId));
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("retrieveDTO error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


	@POST
	@Path("/detail/list")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "list auditLogs")
	public Response listDTO(@ApiParam(value = "param", required = true) QueryUtil queryUtil){
        CommonResultMap map = new CommonResultMap();
        List<AuditLog> auditLogList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            ResetQueryUtil.reset(queryUtil);
            auditLogList = (List<AuditLog>) auditLogServiceImpl.query(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(auditLogList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);

        } catch (Exception e) {
            logger.error("listDTO error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}


}
